<template>
  <PageMain>
    <template #header></template>
    <template #body>
      <div
        class="resource-view-1-box w-full h-full overflow-hidden box-border flex justify-start items-center flex-col"
      >
        <div
          class="content w-full h-[calc(100%_-_64px)] box-border p-[20px_24px] overflow-hidden overflow-y-auto"
        >
          <div
            class="w-full h-[56px] p-[16px_0] overflow-hidden box-border flex justify-start items-start"
            style="font-family: Microsoft YaHei; font-size: 16px; font-weight: bold; color: #394d73"
          >
            资产信息
          </div>
          <div class="info w-full h-auto p-[20px] box-border bg-[#f7f9fc] rounded-[8px]">
            <a-row>
              <a-col :span="8">
                <div class="content">
                  <span class="icon">资源名称 :</span>
                  <span class="text">{{ formInline.asset_package_name }}</span>
                </div>
              </a-col>
              <a-col :span="8">
                <div class="content">
                  <span class="icon">资源编号 :</span>
                  <span class="text">{{ formInline.asset_package_code }}</span>
                </div>
              </a-col>
              <a-col :span="8">
                <div class="content">
                  <span class="icon">资源主体 :</span>
                  <span class="text" v-for="item in formInline.asset_package_owner" :key="item">{{
                    item
                  }}</span>
                </div>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="8">
                <div class="content">
                  <span class="icon">资产分摊金额 :</span>
                  <span class="text bule-text">{{ formInline.actual_allocated_cost }} 元</span>
                </div>
              </a-col>
              <a-col :span="8">
                <div class="content">
                  <span class="icon">资产入表金额 :</span>
                  <span class="text bule-text">{{ formInline.ru_biao_total }} 元</span>
                </div>
              </a-col>
            </a-row>
          </div>
          <div
            class="w-full h-[56px] p-[16px_0] overflow-hidden box-border flex justify-start items-start"
            style="font-family: Microsoft YaHei; font-size: 16px; font-weight: bold; color: #394d73"
          >
            关联凭证
          </div>
          <CommonTable
            :showPagination="false"
            :columnsData="showDetailTableData"
            :tableData="formInline.list_data"
          >
            <template #index="{ rowIndex }">
              <span>{{ rowIndex + 1 }}</span>
            </template>

            <template #actual_allocated_cost="{ record }">
              <TextInput
                width="100%"
                type="text"
                placeholder="请输入金额"
                :disabled="!record.isSave"
                v-model="record.actual_allocated_cost"
              ></TextInput>
            </template>
            <template #ru_biao_status="{ record }">
              <SelectInput width="100%" v-if="record.isSave" v-model="record.ru_biao_status">
                <a-option :value="1">可入表</a-option>
                <a-option :value="2">不可入表</a-option>
              </SelectInput>
              <div v-else>
                <span v-if="record.ru_biao_status === 1" class="flex justify-start items-center">
                  <div class="circle-point border-[#1459FA] mr-1"></div>
                  {{ judgeCorrelationStatus(record.ru_biao_status) }}
                </span>
                <span
                  v-else-if="record.ru_biao_status === 2"
                  class="flex justify-start items-center"
                >
                  <div class="circle-point border-[#DB2C28] mr-1"></div>
                  {{ judgeCorrelationStatus(record.ru_biao_status) }}
                </span>
              </div>
            </template>
            <template #operator="{ record }">
              <a-button type="text" v-if="!record.isSave" @click="costAdjustment(record)"
                >成本调整</a-button
              >
              <a-button type="text" v-else @click="onSave(record)">保存</a-button>
            </template>
          </CommonTable>
        </div>
      </div>
    </template>
  </PageMain>
</template>

<script lang="ts" setup>
import { onMounted, reactive } from 'vue';
import { showDetailTableData } from './values';
import { getDetail, createListDate } from '@/api/costManagement/asset_cost/index';
import { useRoute } from 'vue-router';
import { judgeCorrelationStatus } from '@/util/status';

const route = useRoute();
// 表单数据
const formInline = reactive({
  actual_allocated_cost: '', //资产分摊金额
  asset_package_code: '', //资产编号
  asset_package_name: '', //资产名称
  asset_package_owner: [], //项目编号
  ru_biao_total: '', //项目描述
  list_data: []
});

/**
 * @method costAdjustment 成本调整
 */
const costAdjustment = (item: any) => {
  formInline.list_data?.forEach((itm: any) => {
    if (item.id == (itm.id as any)) {
      itm.isSave = true;
    }
  });
};
/**
 * @method onSave 成本调整
 */
const onSave = async (item: any) => {
  formInline.list_data.forEach((itm: any) => {
    if (item.id == itm.id) {
      itm.isSave = false;
    }
  });
  let res = await createListDate({
    asset_package_id: route.query.id,
    cost_amount: Number(item.actual_allocated_cost),
    id: item.id,
    ru_biao_status: item.ru_biao_status
  });
  if (res.code === '00000') {
    await getDetail(route.query.id as string);
  }
};

const checkDetail = async () => {
  console.log(route.query.id);
  const { data } = await getDetail(route.query.id as string);
  if (!data) return;
  Object.keys(data).forEach((key) => {
    if (formInline.hasOwnProperty(key)) {
      formInline[key] = data[key];
    }
  });
};
onMounted(() => {
  checkDetail();
});
</script>

<style lang="less" scoped>
.MainBox {
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
}
.TabPaneClass {
  padding: 0 30px;
}
.content {
  margin-bottom: 33px;
  font-size: 14px;
  font-family: MicrosoftYaHei;
  color: #465a80;

  .icon {
    color: #a3b0cc;
    font-family: Microsoft YaHei;
    font-size: 14px;
    font-weight: normal;
  }
  .text {
    padding-left: 16px;
    color: #465a80;
    font-family: Microsoft YaHei;
    font-size: 14px;
    font-weight: normal;
  }
  .bule-text {
    color: #1459fa;
  }
}
.footer {
  border-top: 1px solid #ebf0fa;
  width: 100%;
  padding: 0 20px;
  display: flex;
  justify-content: flex-end;
  height: 50px;
  align-items: center;
}
.circle-point {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border-width: 2px;
}
</style>
